<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/14
 * Time: 15:53
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\Product as ProductModel;
use app\api\validate\Count;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\PagingParameter;
use app\lib\exception\ProductException;
use app\lib\exception\ThemeException;

class Product extends BaseController
{
    protected $beforeActionList = [
        'checkSuperScope' => ['only' => 'createOne,deleteOne']
    ];

    /**
     * 获取最近新品
     * @url /product/recent
     * @param int $count
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getRecent($count = 15) {
        $validate = new Count();
        $validate->goCheck();
        $products = ProductModel::getMostRecent($count);
        if($products->isEmpty()){
        }
        $products = $products->hidden(['summary']);
        return $products;
    }
    
    /**
     * 根据分类获得该分类下的所有商品(分页)
     * @url /product/by_category/paginate?id=:category_id&page=:page&size=:page_size
     * @param int $id 分类id
     * @param int $page 分页页数（可选）
     * @param int $size 每页数目（可选）
     * @return array
     */
    public function getByCategory($id = -1,$page = 1,$size = 30){
        (new IDMustBePositiveInt())->goCheck();
        (new PagingParameter())->goCheck();
        $pagingProducts = ProductModel::getProductsByCategoryID($id,true,$page,$size);
        if($pagingProducts->isEmpty()){
            // 对于分页最好不要抛出MissException，客户端并不好处理
            return [
                'current_page' => $pagingProducts->currentPage(),
                'data' => []
            ];
        }
        // 数据集对象和普通二维数组在适用上的一个最大区别就是数据是否为空的判断，
        // 二维数组的数据判空直接使用empty，
        // collection的判空使用$collention->isEmpty()
        $data = $pagingProducts->hidden(['summary'])->toArray();
        return [
            'current_page' => $pagingProducts->currentPage(),
            'data' => $data
        ];
    }
    
    /**
     * 获得某个分类下的全部商品（不分页）
     * @url /product/by_category?id=:category_id
     * @param int $id
     * @return mixed
     * @throws ThemeException
     */
    public function getAllInCategory($id = -1){
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductsByCategoryID($id,false);
        if($products->isEmpty()){
            throw new ThemeException();
        }else{
            $data = $products->hidden(['summary'])->toArray();
            return $data;
        }
    }

    /**
     * 获取商品详情
     * @url /product/:id
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws ProductException
     */
    public function getOne($id){
        (new IDMustBePositiveInt())->goCheck();
        $product = ProductModel::getProductDetail($id);
        if(!$product){
            throw new ProductException();
        }
        return $product;
    }

    public function createOne(){
        $product = new ProductModel();
        $product->save(['id' => 1]);
    }

    public function deleteOne($id){
        ProductModel::destroy($id);
    }
}